<?php

namespace App\Http\Controllers\Admin;

use App\Exports\FangOwnerExport;
use App\Models\FangOwner;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Maatwebsite\Excel\Facades\Excel;

class FangOwnersController extends Controller
{
    /**
     * @param Request $request
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function index(Request $request)
    {
        //
        $where = [];
        //起止时间
        $startTime = $request->get('starttime');
        $endTime = $request->get('endtime');
        $name = $request->get('name');
        if (!empty($startTime) && !empty($endTime)) {
            $where = [
                ['created_at', '>=', $startTime],
                ['created_at', '<=', $endTime]
            ];
        }
        if (!empty($name)) {
            $where[] = ['name', 'like', "%{$name}%"];
        }
        $data = FangOwner::where($where)->paginate(15);
        $params = [
            "startTime" => $startTime,
            "endTime" => $endTime,
            "name" =>$name,
        ];
        return view('admin.fangowers.index',compact('data','params'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
        return view("admin.fangowers.create");
    }

    /**
     * @param Request $request
     * @param FangOwner $fangOwner
     * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
     * @throws \Illuminate\Validation\ValidationException
     */
    public function store(Request $request,FangOwner $fangOwner)
    {
        //
        $this->validate($request, [
            'name' => 'required',
            'age' => 'numeric',
            'phone' => 'phone',
            'email' => 'e-mail',
            'address' => 'required',
        ]);

        if (empty($_FILES['pic']) || count($_FILES['pic']['name']) != 3) {
//            uploadImages('pic');
             return redirect(route("admin.fangowners.create"))->withErrors(['error'=>"身份证上传错误"]);
        }
        $dir = "uploads/fangOwners/" . date('Ymd');
        $images = uploadImages('pic',$dir);
        $pics = implode($images,",");
        $res = $request->except("_token", "url");
        $res['pic'] = $pics;
        $fangOwner::create($res);
        return redirect(route("admin.fangowners.index"));
    }

    /**
     * Display the specified resource.
     *
     * @param  \App\Models\FangOwner  $FangOwner
     * @return \Illuminate\Http\Response
     */
    public function show(FangOwner $FangOwner)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  \App\Models\FangOwner  $FangOwner
     * @return \Illuminate\Http\Response
     */
    public function edit(FangOwner $FangOwner)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \App\Models\FangOwner  $FangOwner
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, FangOwner $FangOwner)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  \App\Models\FangOwner  $FangOwner
     * @return \Illuminate\Http\Response
     */
    public function destroy(FangOwner $FangOwner)
    {
        //
    }

    public function export()
    {
        return Excel::download(new FangOwnerExport(),'房东信息表'.time().'.xlsx');
    }
}
